package com.bupt.ilink.service.bailian.impl;

import com.aliyun.bailian20231229.Client;
import com.aliyun.bailian20231229.models.ListCategoryResponse;
import com.aliyun.tea.TeaException;
import com.bupt.ilink.service.bailian.ListCategoryService;
import com.bupt.ilink.tool.BailianFileTools;
import org.springframework.stereotype.Service;

@Service
public class ListCategoryServiceImpl implements ListCategoryService {

/**
 * 返回格式
 * {
 *   "Status": "200",
 *   "Message": "",
 *   "RequestId": "70BD18FE-7A2E-5BCA-8D72-FB4F742BE942",
 *   "Data": {
 *     "TotalCount": 3,
 *     "CategoryList": [
 *       {
 *         "CategoryId": "cate_37ca0f29c90342f2971e8bd549b7b394_10249160",
 *         "IsDefault": false,
 *         "CategoryName": "test2",
 *         "CategoryType": "UNSTRUCTURED"
 *       },
 *       {
 *         "CategoryId": "cate_5efe750e1ca14337b9f2279485e4fb1a_10249160",
 *         "IsDefault": true,
 *         "CategoryName": "默认类目",
 *         "CategoryType": "UNSTRUCTURED"
 *       },
 *       {
 *         "CategoryId": "cate_ff4dacacc84f429a881803622dd7ce41_10249160",
 *         "IsDefault": false,
 *         "CategoryName": "test",
 *         "CategoryType": "UNSTRUCTURED"
 *       }
 *     ],
 *     "MaxResults": 20,
 *     "HasNext": false
 *   },
 *   "Code": "Success",
 *   "Success": true
 * }
 **/
        public ListCategoryResponse listCategory(Client client, String workspaceId, String categoryType, String parentCategoryId) throws Exception {
            com.aliyun.bailian20231229.models.ListCategoryRequest listCategoryRequest = new com.aliyun.bailian20231229.models.ListCategoryRequest()
                    .setCategoryType(categoryType)
                    .setParentCategoryId(parentCategoryId);
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            java.util.Map<String, String> headers = new java.util.HashMap<>();
            try {
                // 复制代码运行请自行打印 API 的返回值
                return client.listCategoryWithOptions(workspaceId, listCategoryRequest, headers, runtime);
            } catch (TeaException error) {
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                System.out.println(error.getMessage());
                // 诊断地址
                System.out.println(error.getData().get("Recommend"));
                com.aliyun.teautil.Common.assertAsString(error.message);
                throw new TeaException(error.getMessage(), error);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                System.out.println(error.getMessage());
                // 诊断地址
                System.out.println(error.getData().get("Recommend"));
                com.aliyun.teautil.Common.assertAsString(error.message);
                throw new Exception(error.getMessage());
            }
        }

    @Override
    public ListCategoryResponse listCategory(Client client, String workspaceId, String categoryType) throws Exception {
        com.aliyun.bailian20231229.models.ListCategoryRequest listCategoryRequest = new com.aliyun.bailian20231229.models.ListCategoryRequest()
                .setCategoryType(categoryType);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {
            // 复制代码运行请自行打印 API 的返回值
            return client.listCategoryWithOptions(workspaceId, listCategoryRequest, headers, runtime);
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            throw new TeaException(error.getMessage(), error);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            throw new Exception(error.getMessage());
        }
    }

    @Override
    public String getCategoryIdByName(Client client, String workspaceId, String categoryType, String categoryName) throws Exception {
        ListCategoryResponse response = listCategory(client, workspaceId, categoryType);
        if (!BailianFileTools.checkResponse(response)){
            return response.getStatusCode().toString() + "-" + response.getBody().getMessage();
        }
        for (int i = 0; i < response.getBody().getData().getCategoryList().size(); i++) {
            if (response.getBody().getData().getCategoryList().get(i).getCategoryName().equals(categoryName)) {
                return response.getBody().getData().getCategoryList().get(i).getCategoryId();
            }
        }
        return null;
    }

    @Override
    public String getCategoryIdByName(Client client, String workspaceId, String categoryType, String parentCategoryId, String categoryName) throws Exception {
        ListCategoryResponse response = listCategory(client, workspaceId, categoryType, parentCategoryId);
        for (int i = 0; i < response.getBody().getData().getCategoryList().size(); i++) {
            if (response.getBody().getData().getCategoryList().get(i).getCategoryName().equals(categoryName)) {
                return response.getBody().getData().getCategoryList().get(i).getCategoryId();
            }
        }
        return null;
    }


}
